package com.example.elm

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.fragment.app.FragmentActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.example.elm.Account.LoginActivity
import com.example.elm.Public.MyApplication
import com.example.elm.databinding.MainLayoutBinding
import com.google.android.material.bottomnavigation.BottomNavigationView


class MainActivity : FragmentActivity() {
    private lateinit var binding: MainLayoutBinding
    //val server_ip = MyApplication.serverIp

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = MainLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val handler = Handler(Looper.getMainLooper()) // 用于在主线程上运行
        val runnable = object : Runnable {
            override fun run() {
                if (!MyApplication.login) {
                    val intent = Intent(this@MainActivity, LoginActivity::class.java)
                    startActivity(intent)
                    // 延迟 2 秒后再次检查
                    handler.postDelayed(this, 2000)
                }
            }
        }
        // 启动定时任务
        handler.post(runnable)

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        val navController = navHostFragment.navController
        val bottomNavigation = findViewById<BottomNavigationView>(R.id.bottomNavigation)
        bottomNavigation.setupWithNavController(navController)

    }
}